package c01_array;

/**
 * leetcode  11
 *
 */
public class S11 {
    public static void main(String[] args) {
        S11 s11 = new S11();
        int [] arr = new int[]{1,8,6,2,5,4,8,3,7};
        int maxArea = s11.maxArea(arr);
        System.out.println(maxArea);
    }
    public int maxArea(int[] height) {
        int area = maxArea(height, 0, height.length - 1);
        return area;
    }
    public int maxArea(int[] height,int l,int r) {
        if(l==r)  {
            return 0;
        }
        while(l<height.length-2&&height[l+1]<=height[l]) {
            l++;
        }
        while(r>1&&height[r-1]<=height[r]) {
            r--;
        }

        if(l+1==r) {
            return getArea(height,l,r);
        }else {
            return Integer.max(maxArea(height,l+1,r),maxArea(height,l,r-1));
        }

    }

    private int getArea(int[] height, int start,int end) {
        int area = Integer.min(height[start],height[end])*(end-start);
        return area;
    }

}
